package com.mioto.ld.communication.websocket.model;

import cn.hutool.core.util.ObjectUtil;
import com.mioto.ld.communication.websocket.resolver.*;

/**
 * @author mioto-qinxj
 * @date 2024/5/16
 * @desc websocket指令枚举
 */
public enum ReceiveCmdEnum {

    /**
     * 客户端认证
     */
    AUTH(1001,new WsAuthMessageResolver(),WsMessageBody.class),
    /**
     * 属性设置
     */
    CONTROL_ATTRIBUTE_SET(1002,new WsAttributeSetResolver(),WsAttributeSetBody.class),
    /**
     * 远程关机
     */
    CLOSE_DEVICE(1003,new WsCloseResolver(),WsDeviceCloseBody.class),
    /**
     * 不支持的指令
     */
    DEFAULT(0,new WsDefMessageResolver(),WsMessageBody.class);

    public static Class getBodyConvertBean(final Integer cmd){
        return getInstance(cmd).bodyConvertBean;
    }

    public static WsMessageResolver getResolverByCmd(final Integer cmd){
        return getInstance(cmd).resolver;
    }

    private static ReceiveCmdEnum getInstance(Integer cmd){
        ReceiveCmdEnum[] array = ReceiveCmdEnum.values();
        for (ReceiveCmdEnum webSocketCmdEnum : array){
            if (ObjectUtil.equal(cmd,webSocketCmdEnum.cmd)){
                return webSocketCmdEnum;
            }
        }
        return DEFAULT;
    }

    public Integer cmd(){
        return this.cmd;
    }

    private Integer cmd;
    private WsMessageResolver resolver;

    private Class<? extends WsMessageBody> bodyConvertBean;

    ReceiveCmdEnum(Integer cmd, WsMessageResolver resolver,Class<? extends WsMessageBody> bodyConvertBean) {
        this.cmd = cmd;
        this.resolver = resolver;
        this.bodyConvertBean = bodyConvertBean;
    }
}
